package sidebar;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JPanel;

import fileIO.DataBase;
import fileIO.Options;

import mainScreen.MainScreen;
import mainScreen.Profile;
import skin.Skin;
import tools.Enums;
import ui.ImageButton;
import ui.MainWindow;

@SuppressWarnings("serial")
public class Sidebar extends JPanel implements ActionListener{
	Options options;
	//TODO: import from skin
	Enums.hAlignment horAlign = Enums.hAlignment.CENTER;
	int SPACER = 30;
	Enums.SideBarEdge sideBarEdge = Enums.SideBarEdge.RIGHT;
	int SIDEBAR_WIDTH = 200;
	
	MainWindow main; //parent of all. needed for callbacks / screen switch events
	Assistant assistant;
	
	private int activeTab; //index of the currently active tab
	private ImageButton[] buttons;
	private Map<String, MainScreen> screens;
	private Skin skin;
	
	DataBase data;
	
	public Sidebar(Options options, MainWindow main)
	{
		data = new DataBase("Fantasy");
		//TODO: set database at runtime from options
		this.options = options;
		this.main = main; //set the MainWindow parent of everything to a var for reference later
		setLayout(null);
		this.skin = options.skin;

		
		buttons = new ImageButton[10];
		
		spawnButton (0, "Main Menu","Sidebar_MainMenu", this, this, skin);
		spawnButton (1, "Profile", "Sidebar_Profile", this, this, skin);
		spawnButton (2, "Roster",  "Sidebar_Roster", this, this, skin);
		spawnButton (3, "Town",    "Sidebar_Town", this, this, skin);
		spawnButton (4, "Vault",   "Sidebar_Vault", this, this, skin);
		spawnButton (5, "End Week","Sidebar_EndTurn", this, this, skin);		
		spawnButton (6, "Quests",  "Sidebar_Quests", this, this, skin);
		spawnButton (7, "Castle",  "Sidebar_Castle", this, this, skin);
		spawnButton (8, "Tradeskills",  "Sidebar_Tradeskills", this, this, skin);
		spawnButton (9, "Exit",  "Exit", this, this, skin);
		
		activeTab = 0; //main menu is displayed by default
		
		
	}
	
	public void initializeEverything() //initializes all of the screens. In the future consider moving to on-first-use
	{
		//TODO: Implement Assistant / skinning of said assistant
		assistant = new Assistant(skin, this);
		assistant.setBounds(0, 0, SIDEBAR_WIDTH, SIDEBAR_WIDTH);

		this.add(assistant);
		
		
		screens = new HashMap<String, MainScreen>();
		MainScreen tempScreen;
		
		tempScreen = new mainScreen.MainMenu();
		screens.put("MainMenu", tempScreen);
		
		tempScreen = new mainScreen.Castle();
		screens.put("Castle", tempScreen);
		
		tempScreen = new mainScreen.Profile();
		screens.put("Profile", tempScreen);

		tempScreen = new mainScreen.Quests();
		screens.put("Quests", tempScreen);

		tempScreen = new mainScreen.Roster();
		screens.put("Roster", tempScreen);
		
		tempScreen = new mainScreen.Town();
		screens.put("Town", tempScreen);
		
		tempScreen = new mainScreen.Tradeskills();
		screens.put("Tradeskills", tempScreen);
		
		tempScreen = new mainScreen.Vault();
		screens.put("Vault", tempScreen);
		
		tempScreen = new mainScreen.AssistantScreen();
		screens.put("Assistant", tempScreen);
		
		for(MainScreen screen : screens.values())
		{
			screen.initiate(this);
		}
		main.setMainScreen(screens.get("MainMenu"));
	}
	
	private int getButtonX_VertLinear() {
		switch(horAlign) {
			case CENTER: 
				return SIDEBAR_WIDTH/2 - (buttons[0].getWidth()/2);
			case LEFT:   
				return 0;
			case RIGHT:  
				return SIDEBAR_WIDTH - buttons[0].getWidth();
		}	
		return 0;
	}
	private int getButtonY_VertLinear(int buttonIndex) {
		return SPACER*(buttonIndex+3) + SIDEBAR_WIDTH;
	}
	
	private void spawnButton (int buttonIx, String buttonText, String buttonCmd, ActionListener listener, JPanel parentPanel, Skin skin) {
		
		buttons[buttonIx] = new ImageButton (buttonText, true, true, this, 0, 0, skin.button);	
		buttons[buttonIx].coordsX = getButtonX_VertLinear();
		buttons[buttonIx].coordsY = getButtonY_VertLinear (buttonIx);
		
		buttons[buttonIx].setLocation( buttons[buttonIx].coordsX, buttons[buttonIx].coordsY);
		if (buttonCmd != "") {
			buttons[buttonIx].addActionListener(listener);
			buttons[buttonIx].setActionCommand(buttonCmd);
		}
		parentPanel.add(buttons[buttonIx]);
	}	
	
	public void resetTab(String screen)
	{
		int newActiveTab = 0;
		switch(screen)
		{
		case "MainMenu":
			newActiveTab = 0;
			break;
		case "Profile":
			newActiveTab = 1;
			break;
		case "Roster":
			newActiveTab = 2;
			break;
		case "Town":
			newActiveTab = 3;
			break;
		case "Vault":
			newActiveTab = 4;
			break;
		case "EndTurn":
			newActiveTab = 5;
			break;
		case "Quests":
			newActiveTab = 6;
			break;
		case "Castle":
			newActiveTab = 7;
			break;
		case "Tradeskills":
			newActiveTab = 8;
			break;
		
		}
		if(activeTab != newActiveTab)
		{
			buttons[activeTab].setToggledDown(false);
			activeTab = newActiveTab;
		}
	}
	
	public void actionPerformed(ActionEvent evt)
	{
		//the event to be passed in here is to be in the format ScreenName_Action, unless it is exit;
		String[] actions = evt.getActionCommand().split("_");
		switch(actions[0])
		{
		case "Exit":
			//TODO: exit-time cleanups.
			System.exit(0);	//convention sez 0 = no probs
			break;
		case "MainMenu":
			switch(actions[1])
			{
			case "NewGame":
				((Profile) screens.get("Profile")).characterGeneration();
				main.setMainScreen(screens.get("Profile"));
				resetTab(actions[1]);
				break;
			default:
				System.err.println("Action '" + evt.getActionCommand() + "' cannot be handled by the sidebar. (" + actions[0] + ", " + actions[1] + ")");
			}
		case "Sidebar":
			System.out.println(actions[1]);
			resetTab(actions[1]);
			if(actions[1]!="EndTurn")main.setMainScreen(screens.get("Profile"));
			else
			{
				Collection<MainScreen> allScreens = screens.values();
				for(MainScreen curScreen : allScreens)
				{
					curScreen.endTurn();
				}
				//	TODO: Autosave
			}
			break;
		default:
			System.err.println("Action '" + evt.getActionCommand() + "' cannot be handled by the sidebar. (" + actions[0] + ")");
		}
	}
	public Options getOptions()
	{
		return options;
	}
	public void repaintAll()
	{
		this.repaint();
		assistant.repaint();
	}
	public DataBase getDataBase()
	{
		return data;
	}
}
